/**
 * 
 */
package binderon.com.vaadin.admin.user;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import binderon.com.bo.imp.AppUserBoImp;
import binderon.com.bo.imp.model.AppUserBo;

import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.Action;
import com.vaadin.ui.Form;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.CellStyleGenerator;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;

/**
 * @author Nehemia
 * 
 */
public class UserRecords extends VerticalLayout {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private AppUserBoImp appUserBo;
	private Table table = new Table("User records");

	private HashSet<Object> markedRows = new HashSet<Object>();

	static final Action ACTION_MARK = new Action("Mark");
	static final Action ACTION_UNMARK = new Action("Unmark");
	static final Action ACTION_LOG = new Action("Save");
	static final Action[] ACTIONS_UNMARKED = new Action[] { ACTION_MARK,
			ACTION_LOG };
	static final Action[] ACTIONS_MARKED = new Action[] { ACTION_UNMARK,
			ACTION_LOG };

	public UserRecords() {
	}

	@SuppressWarnings({ "serial" })
	public void init() {
		Panel panel = new Panel();
		
		VerticalSplitPanel verticalSplitPanel = new VerticalSplitPanel();
		panel.setContent(verticalSplitPanel);
		panel.setHeight("300px");
		verticalSplitPanel.setFirstComponent(table);

		// Label to indicate current selection
		//final Label selected = new Label("No selection");
		final Form form = new Form();
		form.setCaption("User details:");
		verticalSplitPanel.setSecondComponent(form);

		addComponent(panel);
		// set a style name, so we can style rows and cells
		// table.setStyleName("iso3166");

		// size
		table.setWidth("100%");
		table.setHeight("170px");

		// selectable
		table.setSelectable(true);
		table.setMultiSelect(true);
		table.setImmediate(true); // react at once when something is selected

		// turn on column reordering and collapsing
		table.setColumnReorderingAllowed(true);
		table.setColumnCollapsingAllowed(true);

		// set column headers
		// table.setColumnHeaders(new String[] { "First name", "Last name",
		// "E-mail" });

		// show row header w/ icon
		table.setRowHeaderMode(Table.ROW_HEADER_MODE_EXPLICIT);
		table.addContainerProperty("firstName", String.class, null);
		table.addContainerProperty("lastName", String.class, null);
		table.addContainerProperty("email", String.class, null);
		table.setVisibleColumns(new Object[] { "firstName", "lastName", "email" });

		// Set nicer header names
		table.setColumnHeader("firstName", "First name");
		table.setColumnHeader("lastName", "Last name");
		table.setColumnHeader("email", "E-Mail");

		// Actions (a.k.a context menu)
		table.addActionHandler(new Action.Handler() {
			public Action[] getActions(Object target, Object sender) {
				if (markedRows.contains(target)) {
					return ACTIONS_MARKED;
				} else {
					return ACTIONS_UNMARKED;
				}
			}

			public void handleAction(Action action, Object sender, Object target) {
				if (ACTION_MARK == action) {
					markedRows.add(target);
					table.requestRepaint();
				} else if (ACTION_UNMARK == action) {
					markedRows.remove(target);
					table.requestRepaint();
				} else if (ACTION_LOG == action) {
					Item item = table.getItem(target);
					addComponent(new Label("Saved: " + target + ", "
							+ item.getItemProperty("loginId").getValue()));
				}

			}

		});

		// style generator
		table.setCellStyleGenerator(new CellStyleGenerator() {
			public String getStyle(Object itemId, Object propertyId) {
				if (propertyId == null) {
					// no propertyId, styling row
					return (markedRows.contains(itemId) ? "marked" : null);
				} else if ("loginId".equals(propertyId)) {
					return "bold";
				} else {
					// no style
					return null;
				}

			}

		});

		// listen for valueChange, a.k.a 'select' and update the label
		table.addListener(new Table.ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				// in multiselect mode, a Set of itemIds is returned,
				// in singleselect mode the itemId is returned directly
				Set<?> value = (Set<?>) event.getProperty().getValue();
				if (null == value || value.size() == 0) {
					form.setCaption("User details : No selection");
				} else {
					Set  tableValue = (Set) table.getValue();
					AppUserBo appUserBo = (AppUserBo) tableValue.iterator().next();
					form.setItemDataSource(new BeanItem<AppUserBo>(appUserBo));
					form.setCaption("User details for: " + appUserBo.getLoginId());
				}
			}
		});

	}

	/**
	 * 
	 */
	@SuppressWarnings("rawtypes")
	public void loadData() {
		// connect data source
		List databean = getAppUserBo().getAll();
		BeanItemContainer<AppUserBo> dataSource = new BeanItemContainer<AppUserBo>(
				AppUserBo.class, databean);
	//	dataSource.removeContainerProperty("active");

		table.setContainerDataSource(dataSource);
	}

	/**
	 * @return the appUserBo
	 */
	public AppUserBoImp getAppUserBo() {
		return appUserBo;
	}

	/**
	 * @param appUserBo
	 *            the appUserBo to set
	 */
	public void setAppUserBo(AppUserBoImp appUserBo) {
		this.appUserBo = appUserBo;
	}

}
